import { defHttp } from '/@/utils/http/axios'
import {
  LoginParams,
  LoginResultModel,
  GetUserInfoModel,
  GetUserListSoModel,
  GetUserListModel,
  FormState,
  ListAllUserModel,
} from './model/userModel'

import { RoleInfo, UserRoleModel } from './model/roleModel'

import { BasicFetchResult } from '/@/api/model/baseModel'

import { ErrorMessageMode } from '/#/axios'

enum Api {
  Login = '/login',
  Login1 = '/auth/login',
  Logout = '/auth/logout',
  GetUserInfo1 = '/getUserInfo',
  GetUserInfo = '/user/info',
  GetUserList = '/user/list',
  listAllUser = '/user/list/all',
  GetPermCode = '/getPermCode',
  TestRetry = '/testRetry',
  AddUser = '/user/add',
  DeleteUser = '/user/delete',
  GetUserRoleList = '/user/roleList',
  SaveUserRole = '/user/saveRole',
  updatePassword = '/user/updatePassword',
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login1,
      params,
    },
    {
      errorMessageMode: mode,
    },
  )
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'none' })
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode })
}

export function doLogout() {
  return defHttp.get({ url: Api.Logout })
}

export function getUserList(data: GetUserListSoModel) {
  return defHttp.post<BasicFetchResult<GetUserListModel>>(
    { url: Api.GetUserList, data },
    { errorMessageMode: 'none' },
  )
}

export function testRetry() {
  return defHttp.get(
    { url: Api.TestRetry },
    {
      retryRequest: {
        isOpenRetry: true,
        count: 5,
        waitTime: 1000,
      },
    },
  )
}

export function addUser(data: FormState) {
  return defHttp.post<BasicFetchResult<Boolean>>(
    { url: Api.AddUser, data },
    { errorMessageMode: 'none' },
  )
}

export function deleteUser(params: string) {
  return defHttp.delete<BasicFetchResult<Boolean>>(
    { url: Api.DeleteUser, params: { id: params } },
    { errorMessageMode: 'none' },
  )
}

export function listUserRole(params: string) {
  return defHttp.get<RoleInfo[]>(
    { url: Api.GetUserRoleList, params: { userId: params } },
    { errorMessageMode: 'none' },
  )
}

export function saveUserRole(data: UserRoleModel) {
  return defHttp.post<RoleInfo[]>({ url: Api.SaveUserRole, data }, { errorMessageMode: 'none' })
}

export function listAllUser() {
  return defHttp.get<ListAllUserModel[]>({ url: Api.listAllUser }, { errorMessageMode: 'none' })
}

export function updatePassword(data) {
  return defHttp.get<Boolean>(
    { url: Api.updatePassword, params: { password: data } },
    { errorMessageMode: 'message' },
  )
}
